package com.molyfun.parents.activity.report.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.adapter.PlanDetailTabAdapter;
import com.molyfun.parents.activity.report.plan.PlanDetailCellListView;
import com.molyfun.parents.impl.PlanDetailTabCallback;
import com.molyfun.parents.rest.bean.PlanDetailResponse;
import com.molyfun.parents.utils.LogUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 计划详情View
 */
public class PlanDetailView extends LinearLayout implements PlanDetailTabCallback {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.recyclerViewLeft)
    RecyclerView mRecyclerViewTop;
    @InjectView(R.id.right_container_layout)
    LinearLayout mRightContainerLayout;


    private PlanDetailTabAdapter mPlanDetailTabAdapter;
    //右侧table列表
    private List<PlanDetailResponse.PlanDetailBean.FieldlistBean> mPlanDetailFieldBean;
    private String mPlanId;
    LinearLayoutManager mLinearLayoutManager;

    public PlanDetailView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public PlanDetailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PlanDetailView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_plan_detail, this);
        ButterKnife.inject(this);
    }


    @Override
    public void onPlanDetailTabClick(int position) {
        //点击的item
        if (mPlanDetailFieldBean.get(position).getTemplist().size() == 0) {
            return;
        }
        List<PlanDetailResponse.PlanDetailBean.FieldlistBean.TemplistBean> templist = mPlanDetailFieldBean.get(position).getTemplist();
        LogUtils.d("planDetailView---->" + position + "temlist.size()" + templist.size());
        mRightContainerLayout.removeAllViews();
        if (templist != null && templist.size() > 0) {
            PlanDetailCellListView planDetailCellListView = new PlanDetailCellListView(getContext());
            planDetailCellListView.initData(mPlanId, templist);
            mRightContainerLayout.addView(planDetailCellListView);
        }
    }


    //TODO  更换数据
    public void initData(String planId, List<PlanDetailResponse.PlanDetailBean.FieldlistBean> planDetailFieldBean) {

        if (planDetailFieldBean == null) {
            return;
        }
        mPlanId = planId;
        mPlanDetailFieldBean = planDetailFieldBean;


        mLinearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerViewTop.setLayoutManager(mLinearLayoutManager);
        mRecyclerViewTop.setHasFixedSize(true);

        mPlanDetailTabAdapter = new PlanDetailTabAdapter(context, this);
        mRecyclerViewTop.setAdapter(mPlanDetailTabAdapter);

        mPlanDetailTabAdapter.refreshData(mPlanDetailFieldBean);

        onPlanDetailTabClick(0);

    }
}